package rai.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import rai.util.Constants;

/**
 * Database operation for EditEmpInfo.
 *
 * @author Tien
 *
 */
public class EditEmpInfoDAO {
    /** Log property. */
    private static Logger log = Logger.getLogger("EditEmpInfoDAO.start");

    /**
     * Check Date Format.
     *
     * @param Date
     * @return
     */
    public final String dateFormat(final String Date) {
        String newDate = Constants.BLANK_VALUE;
        for (int i = 0; i < Date.length(); i++) {
            if (i == 1) {
                newDate += Date.charAt(i) + "/";
            } else if (i == 3) {
                newDate += Date.charAt(i) + "/";
            } else {
                newDate += Date.charAt(i);
            }
        }
        log.debug("dateFormat:" + newDate);
        return newDate;
    }

    /**
     * Update Emp Info.
     *
     * @param ID
     * @param Name
     * @param Birthday
     * @param Address
     * @param Email
     * @param Phone
     */
    public void updateEmpInfo(final int ID, final String Name,
            final String Birthday, final String Address, final String Email,
            final String Phone) {
        log.debug("EditEmpInfoDAO.updateEmpInfo.start");
        Connection conn = null;
        String query = Constants.BLANK_VALUE;
        PreparedStatement pStat = null;

        try {
            query = "update employee set Name=?,Birthday=?,Address=?,Email=?,Phone=? where EmpID=?";
            // get connection
            conn = rai.util.Connection.getConnection();
            // prepare statement
            pStat = conn.prepareStatement(query);
            pStat.setString(1, Name);
            pStat.setString(2, dateFormat(Birthday));
            pStat.setString(3, Address);
            pStat.setString(4, Email);
            pStat.setString(5, Phone);
            pStat.setInt(6, ID);
            pStat.executeUpdate();
        } catch (final Exception sqlEx) {
            log.error("EmployeeInfoDAO.getEmpInfo" + sqlEx);
        } finally {
            try {
                pStat.close();
                conn.close();
            } catch (final SQLException sqlEx) {
                log.error("EditEmpInfoDAO.updateEmpInfo " + sqlEx);
            }
        }
        log.debug("EditEmInfoDAO.updateEmpInfo.end");
    }
}
